/**
 * 时间格式化
 * @param {Object} date
 * @param {Object} fmt
 */
export function parseTime(time, pattern) {
	if (arguments.length === 0 || !time) {
		return null
	}
	const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
			time = parseInt(time)
		} else if (typeof time === 'string') {
			time = time.replace(new RegExp(/-/gm), '/')
		}
		if ((typeof time === 'number') && (time.toString().length === 10)) {
			time = time * 1000
		}
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		// Note: getDay() returns 0 on Sunday
		if (key === 'a') {
			return ['日', '一', '二', '三', '四', '五', '六'][value]
		}
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}

/**
 * 回显数据字典
 * @param {Object} datas
 * @param {Object} value
 */
export function selectDictLabel(datas, value) {
	const actions = []
	Object.keys(datas).map((key) => {
		if (datas[key].dictValue == ('' + value)) {
			actions.push(datas[key].dictLabel)
			return false
		}
	})
	return actions.join('')
}


/**
 * 自定义显示消息提示框。
 * @param {Object} msg 内容
 */
export function showMsgToast(msg) {
	uni.showToast({
		title: msg,
		icon: 'none'
	})
}

/**
 * 手机号正则
 * @param {Object} str
 */
export function isValidPhone(str) {
	var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
	return myreg.test(str)
}

/**
 *  判断字符是否为空
 * @param {Object} str
 */
export function isEmpty(str) {
	return str == null || str == '';
}


/**
 * 参数说明：
 * number：要格式化的数字
 * decimals：保留几位小数
 * dec_point：小数点符号
 * thousands_sep：千分位符号
 * */
export function formatPrice(number, decimals, dec_point, thousands_sep) {

	number = (number + '').replace(/[^0-9+-Ee.]/g, '');
	let n = !isFinite(+number) ? 0 : +number,
		prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
		sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
		dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
		s = '',
		toFixedFix = function(n, prec) {
			var k = Math.pow(10, prec);
			return '' + Math.ceil(n * k) / k;
		};

	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	const re = /(-?\d+)(\d{3})/;
	while (re.test(s[0])) {
		s[0] = s[0].replace(re, "$1" + sep + "$2");
	}

	if ((s[1] || '').length < prec) {
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}
/**
 * 计算两个时间差 返回 时分秒
 */
export function timediff(begin_time, end_time) {
	
	if(begin_time&&end_time){
		//年月日时分秒转换为时间戳
		let beginTime = (new Date(begin_time.replace(/\-/g, "/")).getTime()) ;
		let endTime = (new Date(end_time.replace(/\-/g, "/")).getTime()) ;
		var starttime = ''
		var endtime = ''
		if (beginTime < endTime) {
			starttime = beginTime;
			endtime = endTime;
		} else {
			starttime = endTime;
			endtime = beginTime;
		}
		//时间差的毫秒数
		var timediff = (endtime - starttime) /1000;
		
		var leave1 = timediff % (24 * 3600) //计算天数后剩余的毫秒数
		var hours = Math.floor(leave1 / 3600) //计算出小时数
		hours = hours < 10 ? "0" + hours : hours;
		//计算相差分钟数
		var leave2 = leave1 % 3600 //计算小时数后剩余的毫秒数
		var minutes = Math.floor(leave2 / 60) //计算相差分钟数
		minutes = minutes < 10 ? "0" + minutes : minutes;
		//计算相差秒数
		var leave3 = leave2 % 60 //计算分钟数后剩余的毫秒数
		var seconds = Math.round(leave3)
		seconds = seconds < 10 ? "0" + seconds : seconds;
		
		return hours + ':' + minutes + ':' + seconds;
	}else{
		return '00:00:00'
	}
	
}
